//
//  CustomeExtension.swift
//  KJLGT
//
//  Created by jenkin on 2019/8/12.
//  Copyright © 2019年 jenkin. All rights reserved.
//

import Foundation
import SDWebImage

extension String {
    //Range转换为NSRange
    func nsRange(from range: Range<String.Index>) -> NSRange {
        let from = range.lowerBound.samePosition(in: utf16)
        let to = range.upperBound.samePosition(in: utf16)
        return NSRange(location: utf16.distance(from: utf16.startIndex, to: from!),
                       length: utf16.distance(from: from!, to: to!))
    }
    
    //Range转换为NSRange
    func range(from nsRange: NSRange) -> Range<String.Index>? {
        guard
            let from16 = utf16.index(utf16.startIndex, offsetBy: nsRange.location,
                                     limitedBy: utf16.endIndex),
            let to16 = utf16.index(from16, offsetBy: nsRange.length,
                                   limitedBy: utf16.endIndex),
            let from = String.Index(from16, within: self),
            let to = String.Index(to16, within: self)
            else { return nil }
        return from ..< to
    }
}


//extension UIImageView{
//    func setWebImage(url:String, nullType:Int = 0){
//        let image:UIImage = UIImage.init(named: "wx")!
//        self.sd_internalSetImage(with: URL.init(string: url), placeholderImage: image, options: [], context: nil, setImageBlock: nil, progress: nil, completed: nil)
//    }
//}



extension String {
    var containsEmoji: Bool {
        for scalar in unicodeScalars {
            switch scalar.value {
            case
            0x00A0...0x00AF,
            0x2030...0x204F,
            0x2120...0x213F,
            0x2190...0x21AF,
            0x2310...0x329F,
            0x1F000...0x1F9CF:
                return true
            default:
                continue
            }
        }
        return false
    }
}
